import urllib.request
import urllib.parse
import requests
from lxml import etree
import re

from pprint import pprint


def get_response(html_url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
    }
    response = requests.get(url=html_url, headers=headers)
    return response


def get_music_id(context):
    music_id_list = re.findall('<li><a href="/song\?id=(\d+)">(.*?)</a>', context.text)
    # print(music_id_list)  # ('362996', '父亲')
    return music_id_list


#
def save(music_id_list):
    """保存数据"""
    for music_id, name in music_id_list:
        really_save(music_id, name)


def really_save(music_id, name):
    music_url = f'http://music.163.com/song/media/outer/url?id={music_id}.mp3'
    music_content = get_response(html_url=music_url).content
    with open('D:\\deleteAnyTime\\爬虫获取的数据\\网易云music\\' + name + '.mp3', mode='wb') as f:
        f.write(music_content)
        print('保存成功')


if __name__ == '__main__':
    # pass
    url = 'https://music.163.com/discover/toplist?id=19723756'
    context = get_response(html_url=url)
    music_id_list = get_music_id(context)
    save(music_id_list=music_id_list)
